<?php /* $Id: view.php,v 1.27.10.1 2006/11/05 16:04:50 nybod Exp $ */

if (!$canRead) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect();
}

$ticket = dPgetParam( $_GET, 'ticket', '' );
$ticket_type = dPgetParam( $_GET, 'ticket_type', '' );

$type_toggle = dPgetParam( $_POST, 'type_toggle', '' );
$priority_toggle = dPgetParam( $_POST, 'priority_toggle', '' );
$assignment_toggle = dPgetParam( $_POST, 'assignment_toggle', '' );

// setup the title block
$titleBlock = new CTitleBlock( 'View Ticket' );
$titleBlock->addButton( 'tickets list', 'index.php?m=ticketsmith'  );
$titleBlock->addButton( 'my tickets', 'index.php?m=ticketsmith&type=My' );
$titleBlock->addCell();

$titleBlock->show();

require('./modules/ticketsmith/config.inc.php');
require('./modules/ticketsmith/common.inc.php');

/* Centralize references */
$app_root = dPgetConfig( 'base_url' );

/* initialize fields */
if ($ticket_type == 'Staff Followup' || $ticket_type == 'Client Followup') {

    $title = $AppUI->_($ticket_type).' '.$AppUI->_('to Ticket').' #'.$ticket_parent;

    $fields = array('headings' => array('From', 'To', 'Subject', 'Date', 'Cc', '<br />'),
                    'columns'  => array('author', 'recipient', 'subject', 'timestamp', 'cc', 'body'),
                    'types'    => array('email', 'original_author', 'normal', 'elapsed_date', 'email', 'body'));

}
elseif ($ticket_type == 'Staff Comment') {

    $title = $AppUI->_($ticket_type).' '.$AppUI->_('to Ticket').' #'.$ticket_parent;

    $fields = array('headings' => array('From', 'Date', '<br />'),
                    'columns'  => array('author', 'timestamp', 'body'),
                    'types'    => array('email', 'elapsed_date', 'body'));

}
else {

    $title = $AppUI->_('Ticket').' #'.$ticket;

    $fields = array('headings' => array('From', 'Subject', 'Date', 'Cc', 'Status',
                                        'Priority', 'Owner', '<br />'),

                    'columns'  => array('author', 'subject', 'timestamp', 'cc',
                                        'type', 'priority', 'assignment', 'body'),

                    'types'    => array('email', 'normal', 'elapsed_date', 'email',
                                        'status', 'priority_select', 'assignment', 'body'));
}

/* perform updates */
$orig_assignment = dPgetParam( $_POST, 'orig_assignment', '' );
$author = dPgetParam( $_POST, 'author', '' );
$priority = dPgetParam( $_POST, 'priority', '' );
$subject = dPgetParam( $_POST, 'subject', '' );

if (@$type_toggle || @$priority_toggle || @$assignment_toggle) {
    do_query("UPDATE tickets SET type = '$type_toggle', priority = '$priority_toggle', assignment = '$assignment_toggle' WHERE ticket = '$ticket'");
	if(@$assignment_toggle != @$orig_assignment)
	{
		$mailinfo = query2hash("SELECT contact_first_name, contact_last_name, contact_email from users u LEFT JOIN contacts ON u.user_contact = contact_id WHERE user_id = $assignment_toggle");

		if (@$mailinfo['contact_email']) {
			$boundary = "_lkqwkASDHASK89271893712893";
			$message = "--$boundary\n";
			$message .= "Content-disposition: inline\n";
			$message .= "Content-type: text/plain\n\n";
			$message .= $AppUI->_('Trouble ticket assigned to you') . ".\n\n";
			$message .= "Ticket ID: $ticket\n";
			$message .= "Author   : $author\n";
			$message .= "Subject  : $subject\n";
			$message .= "View     : $app_root/index.php?m=ticketsmith&a=view&ticket=$ticket\n";
			$message .= "\n--$boundary\n";
			$message .= "Content-disposition: inline\n";
			$message .= "Content-type: text/html\n\n";
			$message .= "<html>\n";
			$message .= "<head>\n";
			$message .= "<style>\n";
			$message .= ".title {\n";
			$message .= "	FONT-SIZE: 18pt; SIZE: 18pt;\n";
			$message .= "}\n";
			$message .= "</style>\n";
			$message .= "<title>".$AppUI->_('Trouble ticket assigned to you')."</title>\n";
			$message .= "</head>\n";
			$message .= "<body>\n";
			$message .= "\n";
			$message .= "<TABLE border=0 cellpadding=4 cellspacing=1>\n";
			$message .= "	<TR>\n";
			$message .= "	<TD valign=top><img src=$app_root/images/icons/ticketsmith.gif alt= border=0 width=42 height=42></td>\n";
			$message .= "		<TD nowrap><span class=title>".$AppUI->_('Trouble Ticket Management')."</span></td>\n";
			$message .= "		<TD valign=top align=right width=100%>&nbsp;</td>\n";
			$message .= "	</tr>\n";
			$message .= "</TABLE>\n";
			$message .= "<TABLE width=600 border=0 cellpadding=4 cellspacing=1 bgcolor=#878676>\n";
			$message .= "	<TR>\n";
			$message .= "		<TD colspan=2><font face=arial,san-serif size=2 color=white>".$AppUI->_('Ticket assigned to you')."</font></TD>\n";
			$message .= "	</tr>\n";
			$message .= "	<TR>\n";
			$message .= "		<TD bgcolor=white nowrap><font face=arial,san-serif size=2>".$AppUI->_('Ticket ID').":</font></TD>\n";
			$message .= "		<TD bgcolor=white nowrap><font face=arial,san-serif size=2>$ticket</font></TD>\n";
			$message .= "	</tr>\n";
			$message .= "	<TR>\n";
			$message .= "		<TD bgcolor=white><font face=arial,san-serif size=2>".$AppUI->_('Author').":</font></TD>\n";
			$message .= "		<TD bgcolor=white><font face=arial,san-serif size=2>" . str_replace(">", "&gt;", str_replace("<", "&lt;", str_replace('"', '', $author))) . "</font></TD>\n";
			$message .= "	</tr>\n";
			$message .= "	<TR>\n";
			$message .= "		<TD bgcolor=white><font face=arial,san-serif size=2>".$AppUI->_('Subject').":</font></TD>\n";
			$message .= "		<TD bgcolor=white><font face=arial,san-serif size=2>$subject</font></TD>\n";
			$message .= "	</tr>\n";
			$message .= "	<TR>\n";
			$message .= "		<TD bgcolor=white nowrap><font face=arial,san-serif size=2>".$AppUI->_('View').":</font></TD>\n";
			$message .= "		<TD bgcolor=white nowrap><a href=\"$app_root/index.php?m=ticketsmith&a=view&ticket=$ticket\"><font face=arial,sans-serif size=2>$app_root/index.php?m=ticketsmith&a=view&ticket=$ticket</font></a></TD>\n";
			$message .= "	</tr>\n";
			$message .= "</TABLE>\n";
			$message .= "</body>\n";
			$message .= "</html>\n";
			$message .= "\n--$boundary--\n";


			mail($mailinfo["contact_email"], $AppUI->_('Trouble ticket')." #$ticket ".$AppUI->_('has been assigned to you'), $message, "From: " . $CONFIG['reply_to'] . "\nContent-type: multipart/alternative; boundary=\"$boundary\"\nMime-Version: 1.0");
		} // End of check for valid email
	} // End of check for toggle of assignee

} // End of check for change in header fields

/* start table */
?>

<form name="ticketform" action="index.php?m=ticketsmith&a=view&ticket=<?php echo $ticket;?>" method="post">
<input type="hidden" name="ticket" value="$ticket" />

<table class="infopanel" cellspacing="0" cellpadding="3" border="0" width="100%">
<tr>
	<th colspan="2" nowrap="nowrap">
		<?php
			//echo ;
			echo '<table width="100%" border="0" cellspacing="3" cellpadding="0">';
			echo '<tr>'.
				 '<td>'.$title.'</td>'.
				 '<td align="right">';
			if ($ticket_type == 'Staff Followup' || $ticket_type == 'Client Followup' || $ticket_type == 'Staff Comment') {
				if ($canEdit) {
					showIconLink('images/discuss_edit.gif', 'index.php?m=ticketsmith&a=followup&ticket='.$ticket,
						$AppUI->_("Post followup (emails client)"));
					echo '&nbsp;';
					showIconLink('images/discuss_private.gif', 'index.php?m=ticketsmith&a=comment&ticket='.$ticket,
						$AppUI->_('Post internal comment'));
					echo '&nbsp;';
					showIconLink('images/discuss_return.gif', 'index.php?m=ticketsmith&a=view&ticket='.$ticket_parent,
						$AppUI->_('Return to parent'));
				}
				else {
					showIconLink('images/discuss_return.gif', 'index.php?m=ticketsmith&a=view&ticket='.$ticket_parent,
						$AppUI->_('Return to parent'));
				}

			}
			else {
				if ($canEdit) {
					showIconLink('images/discuss_edit.gif', 'index.php?m=ticketsmith&a=followup&ticket='.$ticket,
						$AppUI->_("Post followup (emails client)"));
					echo '&nbsp;';
					showIconLink('images/discuss_private.gif', 'index.php?m=ticketsmith&a=comment&ticket='.$ticket,
						$AppUI->_('Post internal comment'));
				}
			}
			echo '</td>';
			echo '</table>';
		?>
	</th>
</tr>

<?php
/* start form */

/* get ticket */
$ticket_info = query2hash("SELECT * FROM tickets WHERE ticket = $ticket");

echo '<input type="hidden" name="orig_assignment" value="' . $ticket_info['assignment'] . '"/>';
echo '<input type="hidden" name="author" value="' . $ticket_info['author'] . '"/>';
echo '<input type="hidden" name="priority" value="' . $ticket_info['priority'] . '"/>';
echo '<input type="hidden" name="subject" value="' . $ticket_info['subject'] . '"/>';

/* output ticket */
for ($n = 0; $n < count($fields['headings']); $n++) {
	echo '<tr>';
	if ( $fields['headings'][$n] !== '<br />') {
		$fields['headings'][$n] = $AppUI->_($fields['headings'][$n]) .':';
	}
	echo '<td align="right" width="100"><b>' . $fields['headings'][$n] . '</b></td>';
	echo '<td align="left">'.format_field($ticket_info[$fields['columns'][$n]], $fields['types'][$n]) . '</td>';
	echo '</tr>';
}
$ticket_info["assignment"];

/* output attachment indicator */
$attach_count = query2result("SELECT attachment FROM tickets WHERE ticket = '$ticket'");

if ($attach_count == 1) {
	echo '<tr>';
	echo '<td align="left"><b>Attachments</b></td>';
	echo '<td align="left">This email had attachments which were removed.</td>';
	echo '</tr>';
} else if ($attach_count == 2) {
	$result = do_query("SELECT file_id, file_name from files, tickets where ticket = '$ticket'
						and file_task = ticket and file_project = 0");
	if (number_rows($result)) {
		echo '<tr>';
		echo '<td align="right"><b>Attachments:</b></td>';
		echo '<td align="left">';
		while ($row = result2hash($result)) {
			echo '<a href="fileviewer.php?file_id=' . $row['file_id'] . '">'. $row["file_name"].'</a><br>';
		}
		echo '</td></tr>';
	}
}

/* output followup navigation */
if ($ticket_type != 'Staff Followup' && $ticket_type != 'Client Followup' && $ticket_type != 'Staff Comment') {

    /* output followups */
	echo '<tr>';
	echo '<td align="right" valign="top"><b>'.$AppUI->_('Followups').':</b></td>';
	echo '<td align="left" valign="top">';

	/* grab followups */
	$query = "SELECT ticket, type, timestamp, author FROM tickets WHERE parent = '$ticket' ORDER BY ticket " . $CONFIG["followup_order"];
	$result = do_query($query);

	echo '<table id="idFollowups" width="100%" border="0" cellspacing="0" cellpadding="5" class="list">';
	if (number_rows($result)) {
		/* print followups */
		while ($row = result2hash($result)) {
			$action = "location.href='index.php?m=ticketsmith&a=view&ticket={$row['ticket']}'";
			echo '<tr class="'.(@$number++ % 2 == 0 ? 'first' : 'second').'" onclick="'.$action.'" style="cursor: pointer">';
			echo '<td width="16" align="center">'.$number.'</td>';
			$row['author'] = ereg_replace('"', "", $row['author']);
			$row['author'] = htmlspecialchars($row['author']);
			echo '<td>'.$row['author'].'</td>';
			echo '<td>'.$AppUI->_($row['type']).'</td>';
			echo '<td>'.get_time_ago($row["timestamp"]).'</td>';
			echo '</tr>';
		}
	}
	else {
		echo '<tr><td><em>'.$AppUI->_('none').'</em></td></tr>';
	}
	echo '</table>';
	if ($number > 0) {
		echo '<script>Table("idFollowups").setNormalHover();</script>';
	}
	echo '</td></tr>';
}

else {

    /* get peer followups */
    $results = do_query("SELECT ticket, type FROM tickets WHERE parent = '$ticket_parent' ORDER BY ticket " . $CONFIG["followup_order"]);

    /* parse followups */
    while ($row = result2hash($results)) {
        $peer_tickets[] = $row["ticket"];
    }

    /* count peers */
    $peer_count = count($peer_tickets);

    if ($peer_count > 1) {

        /* start row */
        echo '<tr>';
        echo '<td align="right"><b>Followups:</b></td>';

        /* start cell */
        echo '<td valign="middle">';

        /* form peer links */
        for ($n = 0; $n < $peer_count; $n++) {
            if ($peer_tickets[$n] == $ticket) {
                $viewed_peer = $n;
                $peer_strings[$n] = '<span class="pagecurrent">' . ($n + 1) . '</span>';
            }
            else {
                $peer_strings[$n] = '<span class="pagelink">'.
					'<a href="index.php?m=ticketsmith&a=view&ticket='.$peer_tickets[$n].'">'.($n + 1).'</a></span>';
            }
        }

        /* previous navigator */
        if ($viewed_peer > 0) {
            echo '<span class="pagelink">';
			echo '<a href="index.php?m=ticketsmith&a=view&ticket='.$peer_tickets[$viewed_peer - 1].'">';
            echo $CONFIG['followup_order'] == 'ASC' ?  '<img src="images/pprev.gif">' : '<img src="images/pnext.gif">';
            echo '</a></span>&nbsp;';
        }

        /* ticket list */
        echo join('&nbsp;', $peer_strings);

        /* next navigator */
		if ($peer_count - $viewed_peer > 1) {
			echo '&nbsp;<span class="pagelink"><a href="index.php?m=ticketsmith&a=view&ticket='.$peer_tickets[$viewed_peer + 1].'">';
			echo $CONFIG['followup_order'] == 'ASC' ?  '<img src="images/pnext.gif">' : '<img src="images/pprev.gif">';
			echo '</a></span>';
		}

		echo '</td></tr>';
    }

}

/* padding row */
echo '<tr><td colspan="100%"><img src="images/shim.gif" height="1"></td></tr>';

/* end table */
echo '</table>';

/* end form */
echo '</form>';
?>
